﻿using System.IO.IsolatedStorage;

namespace Tequila.Phone.Sharing
{
    /// <summary>
    /// <see cref="IsolatedStorageSettings">IsolatedStorageSettings</see> <see cref="ISettingsService">service</see>.
    /// </summary>
    public class IsolatedStorageApplicationSettingsService : ISettingsService
    {
        private readonly IsolatedStorageSettings _settings = IsolatedStorageSettings.ApplicationSettings;

        /// <summary>
        /// Gets the setting by key.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object Get(string key)
        {
            return _settings[key];
        }

        /// <summary>
        /// Saves the setting with key.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Save(string key, object value)
        {
            if (!Contains(key))
                _settings.Add(key, value);
            else
                _settings[key] = value;
            _settings.Save();
        }

        /// <summary>
        /// Deletes setting by key.
        /// </summary>
        /// <param name="key"></param>
        public void Delete(string key)
        {
            if (Contains(key))
            {
                _settings.Remove(key);
            }
        }

        /// <summary>
        /// Determines whether setting with key persists.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Contains(string key)
        {
            return _settings.Contains(key);
        }
    }
}
